import collegeInfo from "@/api/users/collegeInfo";
import getTrainingPlan, {updateTrainingPlans} from "@/api/students/trainingPlan";

export let methods = {
    async okModal() {
        if (await this.updateTrainingPlan('post')) {
            this.closeModal();
        }
    },
    closeModal() {
        this.trainingPlans = [...this.oldTrainingPlan];
        this.oldTrainingPlan = [];
        this.addTrainingPlanModalShow = false;
    },
    newPlan(type) {
        this.oldTrainingPlan = [...this.trainingPlans];
        if (type === 1) {
            // 基于当前计划做修改
            if (this.trainingPlans.length === 0) {
                this.$message.error("当前培养计划为空");
                return;
            }
        } else if (type === 2) {
            // 新建空的培养计划
            this.trainingPlans = [];
        }
        this.addTrainingPlanModalShow = true;
    },
    loadCollegeInfo() {
        collegeInfo().then(res => {
            if (res.result) {
                this.colleges = res.colleges;
                this.grades = res.grades;
                this.grades.push({
                    id: -1,
                    text: "xxxx级"
                })
                // 此处只需展示两级，不需要班级信息，故需要删除
                for (let college of this.colleges) {
                    for (let major of college.children) {
                        major.children = undefined
                    }
                }
            } else {
                this.$message.error(res.msg);
            }
        })
    },
    changeMajor(data) {
        this.majorId = data[1];
    },
    loadTrainingPlan() {
        if (!this.majorId || this.gradeId === -1) {
            this.$message.error("请选择专业和年级");
            return;
        }
        getTrainingPlan({
            majorId: this.majorId,
            gradeId: this.gradeId
        }).then(res => {
            if (res.result) {
                this.trainingPlans = res.trainingPlans;
                this.trainingPlanName = res.name;
                this.cacheData = this.trainingPlans.map(item => ({...item}));
            } else {
                this.$message.error(res.msg);
            }
        });
    },
    addRow() {
        if (this.majorId === "" || this.gradeId === -1) {
            this.$message.error("年级或专业为空");
            return;
        }
        if (this.editingKey !== "") {
            this.$message.error("当前行正在编辑");
            return;
        }
        let length = this.trainingPlans.length;
        let newRow = {
            key: length,
            semester: "",
            courseId: "",
            name: "",
            direction: "",
            type: "",
            classHour: "",
            credit: "",
            optional: "",
            remarks: "",
            editable: true,
        }
        this.trainingPlans.push(newRow);
        this.cacheData.push(newRow);
        this.editingKey = length;
    },
    deleteRow(record) {
        const trainingPlans = [...this.trainingPlans];
        this.trainingPlans = trainingPlans.filter(item => item.key !== record.key);
    },
    async updateTrainingPlan(method) {
        if (!this.majorId || this.gradeId === -1) {
            this.$message.error("年级或专业为空");
            return false;
        }
        if (this.trainingPlans.length === 0) {
            this.$message.error("培养计划为空");
            return false;
        }
        const res = await updateTrainingPlans(this.majorId, this.gradeId, this.trainingPlans, method);
        if (res.result) {
            this.$message.success("操作成功");
        } else {
            this.$message.error(res.msg);
        }
        return res.result;
    },
    textChange(value, key, column) {
        // input框数据修改2
        let dt = this.getDataAndTarget(key);
        if (dt.target) {
            dt.target[column] = value;
            this.trainingPlans = dt.newData;
        }
    },
    save(key) {
        //  保存一行的修改
        let dt = this.getDataAndTarget(key);
        const newCacheData = [...this.cacheData];
        const targetCache = newCacheData.filter(item => key === item.key)[0];
        if (dt.target && targetCache) {
            delete dt.target.editable;
            this.trainingPlans = dt.newData;
            Object.assign(targetCache, dt.target);
            this.cacheData = newCacheData;
        }
        this.editingKey = '';
    },
    cancel(key) {
        // 取消一行的修改
        let dt = this.getDataAndTarget(key);
        this.editingKey = "";
        if (dt.target) {
            Object.assign(dt.target, this.cacheData.filter(item => key === item.key)[0]);
            delete dt.target.editable;
            this.trainingPlans = dt.newData;
        }
    },
    edit(key) {
        let dt = this.getDataAndTarget(key);
        this.editingKey = key;
        if (dt.target) {
            dt.target.editable = true;
            this.trainingPlans = dt.newData;
        }
    },
    getDataAndTarget(key) {
        const newData = [...this.trainingPlans];
        const target = newData.filter(item => key === item.key)[0];
        return {
            newData, target
        }
    }
}